package cn.smdq.ch.auth.center.config;

import cn.smdq.ch.common.core.config.properties.AuthIgnoreUrlProperties;
import cn.smdq.ch.common.util.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter;

import java.util.List;

/**
 * @Description 安全配置
 * @Author shuimodanqing
 * @Date 2019/1/17  3:44 PM
 */
@Configuration
@EnableConfigurationProperties(AuthIgnoreUrlProperties.class)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthIgnoreUrlProperties ignoreUrlProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/auth/require")
                .loginProcessingUrl("/auth/form")
                .and()
                .authorizeRequests()
                .antMatchers(ArrayUtils.toStrArray(ignoreUrlProperties.getUrls())).permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    //ClientCredentialsTokenEndpointFilter
}
